<template>
	<common-card type="audit" title="课题审核" :loading="loading">
		<detail-body :data="dataDetail"></detail-body>
		<div class="audit_form_wrapper">
			<div class="label">审核意见</div>
			<el-form ref="auditForm" size="medium" :model="auditData" :rules="auditRules">
				<el-form-item class="audit" prop="audit">
					<el-radio-group v-model="auditData.audit" v-removeAriaHidden>
						<el-radio class="pass" label="1">通过</el-radio>
						<el-radio class="reject" label="2">不通过</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item class="content" prop="auditDesc">
					<el-input type="textarea" :rows="6" placeholder="请输入审批意见" v-model="auditData.auditDesc"></el-input>
				</el-form-item>
			</el-form>
		</div>
		<template slot="button">
			<el-button type="success" icon="sw-icon-btn-audit" :loading="btnLoading" @click="handleAuditData()">确定</el-button>
			<el-button @click="$router.back()">取消</el-button>
		</template>
	</common-card>
</template>

<script>
import mixins from './mixins';
import detailBody from './body';
import commonCard from '../commonCard';
import { topicDetail, topicAudit } from '@/api/business/tbTopic';
export default {
	mixins: [mixins],
	components: { commonCard, detailBody },
	props: {
		userRole: { type: String, default: '' },
		dataType: { type: String, default: '' }
	},
	data() {
		return {
			loading: false,
			dataDetail: {},
			auditData: {},
			auditRules: {
				audit: [{ required: true, message: '请选择审核状态', trigger: 'change' }]
			},
			btnLoading: false
		};
	},
	computed: {
		dataId() {
			return this.$route.query.id || '';
		}
	},
	watch: {
		dataId: {
			handler(id) {
				this.loading = true;
				this.getDataDetail(id);
			},
			immediate: true
		}
	},
	methods: {
		//获取数据详情
		async getDataDetail(id) {
			let result = await topicDetail(id).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				let datas = this.$detailData(result.data);
				this.$set(datas, 'piList', this.returnCheckedPiData(datas.piList || []));
				this.$set(datas, 'fundsList', this.returnCheckedFund(datas.fundsList || []));
				this.$set(datas, 'collaList', this.returnCheckedPerson(datas.collaList || []));
				this.$set(datas, 'measureList', this.returnCheckedMeasure(datas.measureList || []));
				this.dataDetail = datas;
			}
		},
		//审核数据
		handleAuditData() {
			let descRule = [{ required: true, message: '请输入不通过理由', trigger: 'blur' }];
			this.$refs['auditForm'].clearValidate();
			if (this.auditData.audit === '2') {
				this.$set(this.auditRules, 'auditDesc', descRule);
			} else {
				this.$delete(this.auditRules, 'auditDesc');
			}
			this.$refs['auditForm'].validate((valid) => {
				if (!valid) return false;
				this.btnLoading = true;
				this.auditDataRequest();
			});
		},

		//审核请求
		async auditDataRequest() {
			let params = { id: this.dataId, ...this.auditData };
			let result = await topicAudit(params).then((res) => res.data);
			this.btnLoading = false;
			if (result.code === '0') {
				this.$router.back();
				this.$showCustomMessage('success', '审核成功');
			}
		}
	}
};
</script>

<style lang="scss" scoped>
::v-deep.common_card_wrapper .button {
	justify-content: flex-start;
}
</style>
